package org.phalanx.client.mapeditor;

import java.awt.Color;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import org.phalanx.client.UI.PhalanxIconButton;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;
import org.phalanx.client.menus.DesignMenuPopUp;
import org.phalanx.client.menus.DisplayMenuPopUp;
import org.phalanx.client.menus.MapMenuPopUp;

public class PhalanxEditorTopToolBarPanel extends JPanel implements
		pButtonListener {
	private PhalanxMapEditorPanel editorPanel;
	private PhalanxIconButton mapMenu;
	private PhalanxIconButton displayMenu;
	private PhalanxIconButton designMenu;
	private MapMenuPopUp mapMenuPopUp;
	private DisplayMenuPopUp displayMenuPopUp;
	private DesignMenuPopUp designMenuPopUp;

	public PhalanxEditorTopToolBarPanel(PhalanxMapEditorPanel edit) {
		this.editorPanel = edit;
		initializePanel();
		createMenus();
		setToolBarLayout();
	}

	private void initializePanel() {
		this.setBackground(Color.black);
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
		createToolBarButtons();
	}

	private void createMenus() {
		mapMenuPopUp = new MapMenuPopUp(editorPanel);
		displayMenuPopUp = new DisplayMenuPopUp(editorPanel);
		designMenuPopUp = new DesignMenuPopUp(editorPanel);
	}

	private void createToolBarButtons() {

		mapMenu = new PhalanxIconButton();
		mapMenu.setText("Map");
		mapMenu.setForeground(Color.white);
		mapMenu.setDefaultIcon(new ImageIcon(
				"./resources/images/toolbars/MapMenu.png"));
		mapMenu.setHoveredIcon(new ImageIcon(
				"./resources/images/toolbars/MapMenuHovered.png"));
		mapMenu.setClickedIcon(mapMenu.getIcon(PhalanxIconButton.DEFAULT_ICON));
		mapMenu.addButtonListener(this);

		displayMenu = new PhalanxIconButton();
		displayMenu.setText("Display");
		displayMenu.setForeground(Color.white);
		displayMenu.setDefaultIcon(new ImageIcon(
				"./resources/images/toolbars/DisplayMenu.png"));
		displayMenu.setHoveredIcon(new ImageIcon(
				"./resources/images/toolbars/DisplayMenuHovered.png"));
		displayMenu.setClickedIcon(displayMenu
				.getIcon(PhalanxIconButton.DEFAULT_ICON));
		displayMenu.addButtonListener(this);

		designMenu = new PhalanxIconButton();
		designMenu.setText("Design");
		designMenu.setForeground(Color.white);
		designMenu.setDefaultIcon(new ImageIcon(
				"./resources/images/toolbars/DesignMenu.png"));
		designMenu.setHoveredIcon(new ImageIcon(
				"./resources/images/toolbars/DesignMenuHovered.png"));
		designMenu.setClickedIcon(designMenu
				.getIcon(PhalanxIconButton.DEFAULT_ICON));
		designMenu.addButtonListener(this);
	}

	private void setToolBarLayout() {
		this.add(Box.createHorizontalStrut(40));
		this.add(mapMenu);
		this.add(Box.createHorizontalStrut(20));
		this.add(displayMenu);
		this.add(Box.createHorizontalStrut(20));
		this.add(designMenu);
	}

	public JPanel getMasterPanel() {
		return editorPanel;
	}

	public void actionPerformed(pButtonPressed p) {
		if (p.getComponent().equals(mapMenu)) {
			mapMenuPopUp.show(this, getPopUpPositionX(mapMenu),
					getPopUpPositionY(mapMenu));
		} else if (p.getComponent().equals(displayMenu)) {
			displayMenuPopUp.show(this, getPopUpPositionX(displayMenu),
					getPopUpPositionY(displayMenu));
		} else if (p.getComponent().equals(designMenu)) {
			designMenuPopUp.show(this, getPopUpPositionX(designMenu),
					getPopUpPositionY(designMenu));
		}
	}

	private Integer getPopUpPositionX(PhalanxIconButton p) {
		return p.getX() + (p.getWidth() / 2);
	}

	private Integer getPopUpPositionY(PhalanxIconButton p) {
		return p.getY() + p.getHeight();
	}
}
